package org.fireflow2.designer.eclipse.editors;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.EditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.gef.ui.parts.GraphicalEditorWithPalette;
import org.eclipse.gef.ui.parts.SelectionSynchronizer;
import org.eclipse.ui.IWorkbenchPart;
import org.fireflow2.designer.eclipse.actions.AddDataFieldAction;
import org.fireflow2.designer.eclipse.actions.AddResourceDefAction;
import org.fireflow2.designer.eclipse.actions.DesignPanelContextMenuProvider;
import org.fireflow2.designer.eclipse.actions.ShowPropertyViewAction;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.palette.PaletteFactory;
import org.fireflow2.designer.eclipse.parts.PartFactory;


public class WorkflowProcessDesignerPanel extends GraphicalEditorWithPalette{ 
//	implements ITabbedPropertySheetPageContributor{
	SubProcessWrapper subProcessWrapper = null;
	PaletteRoot paletteRoot = null;
	FireflowMultiPageEditor parentEditor = null;

	public WorkflowProcessDesignerPanel(SubProcessWrapper processWrapper,FireflowMultiPageEditor pedt) {
		super();
		setEditDomain(new DefaultEditDomain(this));
		subProcessWrapper = processWrapper;
		parentEditor = pedt;
	}

    public GraphicalViewer getTheGraphicalViewer(){
    	return this.getGraphicalViewer();
    }
	
	@Override
	protected PaletteRoot getPaletteRoot() {
		// TODO Auto-generated method stub
//		System.out.println("----------the workflowProcess is "+this.workflowProcess.hashCode());
		if (this.paletteRoot == null) {
			this.paletteRoot = PaletteFactory.createPalette();
		}
		
		return this.paletteRoot;
	}

	protected void configureGraphicalViewer() {
		super.configureGraphicalViewer();
		getGraphicalViewer()
				.setRootEditPart(new ScalableFreeformRootEditPart());
		getGraphicalViewer().setEditPartFactory(new PartFactory(true));
		
		
		DesignPanelContextMenuProvider contextMenuProvider = new DesignPanelContextMenuProvider(this.getGraphicalViewer(),this.getActionRegistry());
		this.getGraphicalViewer().setContextMenu(contextMenuProvider);
	}

	class TmpLayer extends FreeformLayer{
		public TmpLayer(){
			setOpaque(false);
			
		}
	}
	@Override
	protected void initializeGraphicalViewer() {
		super.initializePaletteViewer();
		this.getGraphicalViewer().setContents(subProcessWrapper);
	}

	@Override
	public void doSave(IProgressMonitor arg0) {
		// TODO Auto-generated method stub

	}

	public void updateSelectionActions(){
		updateActions(getSelectionActions());	
	}

	protected void createActions() {
		super.createActions();

		SelectionAction action = new ShowPropertyViewAction((IWorkbenchPart)this);
		this.getActionRegistry().registerAction(action);
		getSelectionActions().add(action.getId());
		this.getStackActions().add(action.getId());
		
//		SelectionAction action = new AddFormTaskAction((IWorkbenchPart)this);
//		this.getActionRegistry().registerAction(action);
//		getSelectionActions().add(action.getId());
//		this.getStackActions().add(action.getId());
//		
//		action = new AddToolTaskAction((IWorkbenchPart)this);
//		this.getActionRegistry().registerAction(action);
//		getSelectionActions().add(action.getId());
//		this.getStackActions().add(action.getId());
//		
//		action = new AddSubflowTaskAction((IWorkbenchPart)this);
//		this.getActionRegistry().registerAction(action);
//		getSelectionActions().add(action.getId());
//		this.getStackActions().add(action.getId());
//		
//		action = new AddTaskReferenceAction((IWorkbenchPart)this);
//		this.getActionRegistry().registerAction(action);
//		getSelectionActions().add(action.getId());
//		this.getStackActions().add(action.getId());
		
		action = new AddDataFieldAction((IWorkbenchPart)this);
		this.getActionRegistry().registerAction(action);
		getSelectionActions().add(action.getId());	
		this.getStackActions().add(action.getId());
		
		action = new AddResourceDefAction((IWorkbenchPart)this);
		this.getActionRegistry().registerAction(action);
		getSelectionActions().add(action.getId());	
		this.getStackActions().add(action.getId());
	}
	
	/*
	protected void setInput(IEditorInput input) {
		super.setInput(input);

	}
	*/
	

	@Override
	public Object getAdapter(Class type) {
        if (type==SelectionSynchronizer.class){
        	return this.getSelectionSynchronizer();
        }
        if (type==EditDomain.class){
        	return this.getEditDomain();
        }
        
        return super.getAdapter(type);
	}

/*	
	class OutlinePage extends ContentOutlinePage {
	    private Control outline;

	    public OutlinePage() {
	        super(new TreeViewer());        
	    }

	    public void init(IPageSite pageSite) {
	        super.init(pageSite);
	        ActionRegistry registry = getActionRegistry();
	        IActionBars bars = pageSite.getActionBars();
	        String id = IWorkbenchActionConstants.UNDO;
	        bars.setGlobalActionHandler(id, registry.getAction(id));
	        id = IWorkbenchActionConstants.REDO;
	        bars.setGlobalActionHandler(id, registry.getAction(id));
	        id = IWorkbenchActionConstants.DELETE;
	        bars.setGlobalActionHandler(id, registry.getAction(id));
	        bars.updateActionBars();
	        
	       
	    }

	    public void createControl(Composite parentWrapper) {
	        outline = getViewer().createControl(parentWrapper);
	        getSelectionSynchronizer().addViewer(getViewer());
	        getViewer().setEditDomain(getEditDomain());
	        getViewer().setEditPartFactory(new TreePartFactory());
	        //            getViewer().setKeyHandler(getCommonKeyHandler());
	        List list = workflowProcessWrapper.getAllNodes4OutlineView();

	        getViewer().setContents(workflowProcessWrapper);
	    }

	    public Control getControl() {
	        //            return pageBook;

	        return outline;
	    }

	    public void dispose() {
	        getSelectionSynchronizer().removeViewer(getViewer());
	        super.dispose();
	    }


	}*/
	

	protected void hookGraphicalViewer()
	{
		super.hookGraphicalViewer();

		parentEditor.getSelectionSynchronizer().addSelectionProvider(this.getGraphicalViewer());
	}

}
